const db = wx.cloud.database();
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		// 订单信息
		lists: {
			info: {
				name: '',
				number: '',
				address: '',
				submitTime: '',
				sendTime: '',
				pay: '',
				sendPrice: 0,
				totalPrice: 0,
				note: '',
				type: ''
			},
			card: {}
		},
		// 备注信息
		notes: '',
		// 是否商家自送
		isSend: true,
		// 总价
		price: 0,
		// 商品列表
		shoppingList: [],
		// 店铺列表
		shop: [],
		// Tab栏
		currentTabIndex: 0,
		// 小计
		sumitPrice: 0,
		userName: '未命名',
		sex: '*',
		telNumber: '**',
		// 地址信息
		area: '',
		side: '',
		count: '',
		room: '',
		chooseAddress: false,
		chooseTime: false,
		chooseTakeTime: false,
		time: '选择配送时间',
		wechatPay: 'primary',
		pau: 'default',
		currentDate: '12:00',
		sumitbarShow: false,
		filter(type, options) {
			if (type === 'minute') {
				return options.filter((option) => option % 10 === 0);
			}

			return options;
		},
		timeList: [{
				values: [{
						text: '今天',
						disabled: false
					},
					{
						text: '明天',
						disabled: false
					}
				],
				defaultIndex: 0
			},
			{
				values: [{
						text: '12：00 — 13:30',
						disabled: false
					},
					{
						text: '18：00 — 19:30',
						disabled: false
					},
					{
						text: '20：00 — 22:00',
						disabled: false
					}
				],
				defaultIndex: 1
			},
		],
		areaList: {
			province_list: {
				110000: '宿舍区',
				120000: '教学区',
				130000: '其他',
			},
			city_list: {
				110100: '1-4栋(女生)',
				110200: '5-8栋(女生)',
				110300: '9-12栋(女生)',
				110400: '13-16栋(男生)',
				110500: '17-20栋(女生)',
				110600: '21-24栋(男生)',
				110700: '25-26栋(女生)',
				110800: '四合院(男生)',

				120100: '教学楼',
				120200: '信息楼',
				120300: '实训楼',
				120400: '医学楼',
				120500: '化工楼',
				120600: '设计楼',

				130100: '创培楼',
				130200: '行政楼',
				130300: '东门',
				130400: '正门',
			},
			county_list: {
				110101: '1栋',
				110102: '2栋',
				110103: '3栋',
				110104: '4栋',
				110201: '5栋',
				110202: '6栋',
				110203: '7栋',
				110204: '8栋',
				110301: '9栋',
				110302: '10栋',
				110303: '11栋',
				110304: '12栋',
				110401: '13栋',
				110402: '14栋',
				110403: '15栋',
				110404: '16栋',
				110501: '17栋',
				110502: '18栋',
				110503: '19栋',
				110504: '20栋',
				110601: '21栋',
				110602: '22栋',
				110603: '23栋',
				110604: '24栋',
				110701: '25栋',
				110702: '26栋',
				110801: '27栋',
				110802: '28栋',
				110803: '29栋',
				110804: '30栋',

				120101: '1栋',
				120102: '2栋',
				120103: '3栋',
				120104: '4栋',
				120105: '5栋',
				120106: '6栋',
				120107: '7栋(菜鸟驿站)',
				120108: '8栋(墨子像)',
				120109: '9栋',
				120110: '10栋',
				120111: '11栋(7号报告厅)',

				120201: '信息主楼',
				120202: '信息副楼',

				120301: '1栋',
				120302: '2栋',
				120303: '3栋',
				120304: '4栋',
				120305: '5栋',
				120306: '6栋',
				120307: '7栋',
				120308: '8栋',
				120309: '9栋',
				120310: '10栋'
			}
		},
	},
	submitLists: function() {
		let lists = {
			info: {
				name: this.data.userName,
				number: this.data.telNumber,
				address: this.data.area + this.data.side + this.data.count + this.data.room,
				submitTime: new Date().toLocaleString(),
				sendTime: this.data.time,
				pay: '微信支付',
				sendPrice: this.data.shop.business ? 1 : 1.5,
				totalPrice: (this.data.price / 100).toFixed(2),
				type: this.data.currentTabIndex == 0 ? '外卖' : '自提',
				notes: this.data.notes,
				shopName: this.data.shop.shopName
			},
			card: this.data.shoppingList
		}
		console.log(lists)
		db.collection('lists').add({
			data: lists
		})
		wx.switchTab({
			url: `../order/order`
		})

	},
	getnotes: function(e) {
		console.log(e.detail)
		this.setData({
			notes: e.detail
		})
	},
	getroom: function(e) {
		console.log(e.detail)
		this.setData({
			room: e.detail
		})
	},
	// 送货方式
	onTabsItemTap: function(event) {
		let index = 0
		// console.log(event)
		if (event != 0)
			index = event.currentTarget.dataset.index

		// console.log(index)
		// console.log(parseFloat(this.data.sumitPrice) + 1.5)
		let price = 0
		let a = this.data.shop.business ? 1 : 1.5
		if (index == 0) {
			price = (parseFloat(this.data.sumitPrice) + a).toFixed(2)
		} else price = parseFloat(this.data.sumitPrice).toFixed(2)
		this.setData({
			currentTabIndex: index,
			price: price * 100
		})
		// console.log(price)
		// this.count()
	},
	changeInfo: function() {
		wx.navigateTo({
			url: '../myInfo/myInfo'
		});
	},
	chooseTakeTime: function() {
		this.setData({
			chooseTakeTime: true,
			sumitbarShow: true
		})
	},
	chooseAddress: function() {
		this.setData({
			chooseAddress: true,
			sumitbarShow: true
		})
	},
	chooseTime: function() {
		this.setData({
			chooseTime: true,
			sumitbarShow: true
		})
	},
	getDate: function(e) {
		let timeList = this.data.timeList;
		if (e.detail.value[0].text == '明天') {
			timeList[1].values[0].disabled = false;
			timeList[1].values[1].disabled = false;
			timeList[1].values[2].disabled = false;
			this.setData({
				timeList: timeList
			})
		} else if (e.detail.value[0].text == '今天') {
			this.setTime();
		}
	},
	setTime: function() {
		let thisHours = new Date().getHours(); //获取当前小时数(0-23)
		let thisMinutes = new Date().getMinutes(); //获取当前分钟数(0-59)
		let timeList = this.data.timeList;
		if (thisMinutes < 30) {
			let currentDate = thisHours + ':' + (parseInt(thisMinutes) + 30)
			this.setData({
				currentDate: currentDate
			})
		} else if (thisMinutes > 30) {
			let currentDate = (parseInt(thisHours) + 1) + ':' + (parseInt(thisMinutes) - 30)
			this.setData({
				currentDate: currentDate
			})
		};
		if (thisHours <= 12) {
			if (thisMinutes < 50) {
				timeList[1].defaultIndex = 0;
				this.setData({
					time: '今天12：00 — 13:30',
					timeList: timeList
				})
			}
		} else if (thisHours <= 18) {
			if (thisMinutes < 50) {
				timeList[1].defaultIndex = 1;
				timeList[1].values[0].disabled = true;
				this.setData({
					time: '今天18：00 — 19:30',
					timeList: timeList
				})
			}
		} else if (thisHours < 22) {
			if (thisMinutes <= 59) {
				timeList[1].defaultIndex = 2;
				timeList[1].values[0].disabled = true;
				timeList[1].values[1].disabled = true;
				this.setData({
					time: '今天20：00 — 22:30',
					timeList: timeList
				})
			}
		} else if (thisHours >= 22) {
			timeList[1].defaultIndex = 0;
			timeList[0].defaultIndex = 1;
			timeList[0].values[0].disabled = true;
			this.setData({
				time: '请选择配送时间',
				timeList: timeList
			})
		};
	},
	gotTime: function(e) {
		let event = e.detail.value;
		console.log(event)
		this.setData({
			time: event[0].text + event[1].text,
			chooseTime: false
		})
	},
	gotAddress: function(e) {
		let event = e.detail.values;
		console.log(event)
		if (event[0].name != '') {
			if (event[1].name != '') {
				this.setData({
					chooseAddress: false,
					area: event[0].name ? event[0].name : '',
					side: event[1].name ? event[1].name : '',
					count: event[2].name ? event[2].name : ''
				})
			} else {
				wx.showToast({
					title: '请选择完整地址',
					icon: 'none'
				})
			}
		} else {
			wx.showToast({
				title: '请选择完整地址',
				icon: 'none'
			})
		}
		console.log(this.data.area)
		this.closePop()
	},
	closePop: function() {
		this.setData({
			chooseAddress: false,
			chooseTime: false,
			chooseTakeTime: false,
			sumitbarShow: false
		})
	},
	wechatPay: function() {
		this.setData({
			wechatPay: 'primary',
			pay: 'default'
		})
	},
	pay: function() {
		this.setData({
			wechatPay: 'default',
			pay: 'primary'
		})
	},
	// 计算价格
	count: function() {
		let shoppingList = this.data.shoppingList
		let totalPrice = 0
		for (let i = 0; i < shoppingList.length; i++) {
			totalPrice += parseInt(shoppingList[i].commodity_price) + 1
		}
		let currentTabIndex = this.data.currentTabIndex;
		if (currentTabIndex == 1) {
			let price = parseInt(totalPrice.toString() + '00')
			let isSend = false;
		} else if (currentTabIndex == 0) {
			if (this.data.shop.isSend) {
				let price = parseFloat((totalPrice + 1) * 100)
			} else {
				let price = parseFloat((totalPrice + 1.5) * 100)
				console.log(price)
			}
			let isSend = true;
		}
		this.setData({
			shoppingList: shoppingList,
			totalPrice: price,
			isSend: isSend
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log(options.shopId)
		// console.log(options.shopcar)
		// 获取商品
		this.setData({
			shoppingList: JSON.parse(options.shopcar),
			sumitPrice: (options.sumitPrice / 100).toFixed(2)
		})
		// console.log(this.data.shoppingList)
		// 获取店铺信息
		db.collection('shop').doc(options.shopId).get({
			success: (res) => {
				// console.log(res.data)
				this.setData({
					shop: res.data
				})
				// console.log(this.data.shop)
				// console.log(this.data.shop.avatarUrl)
				// console.log(this.data.shop.shopName)
			}
		})

		this.onTabsItemTap(0)
		this.setTime()
		// 获取用户信息
		console.log(wx.getStorageSync('openid'))
		db.collection('user').where({
			_openid: wx.getStorageSync('openid') // 填入当前用户 openid
		}).get({
			success: (res) => {
				//console.log(res)
				this.setData({
					userName: res.data[0].name,
					sex: (res.data[0].sex),
					telNumber: res.data[0].telephone,
					area: res.data[0].address,
				})
			}
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
